#!/usr/bin/perl -w
#
#

use lib qw(../lib);
use Data::Dumper;

use strict;

#use Encode qw(encode decode);
use Config::General;
use MTT::Schema;

my $sw_file = $ARGV[0] || die 'switchs file need';
my %cfg = new Config::General('../mtt.conf')->getall();

my $schema = MTT::Schema->connect( $cfg{'Model::DBIC'}->{connect_info} );

open(F, $sw_file) || die $!;
my @switches = <F>;
close F;
shift @switches if $switches[0] =~ /HOST_CODE/i;

my %types = map { my ($type) = $_ =~ /;(\w+)\s*$/; $type => 1 } grep { /^.+;.+;.+$/ } @switches;

foreach my $name ( keys %types ) {
	my $type = $schema->resultset('SwitchType')->update_or_create({type => $name});
	$types{$name} = $type->id;			
}

foreach ( @switches ) {
	my ($host_code, $descr, $type) = split /;/;
	$type =~ s/^\s+//;
	$type =~ s/\s+$//;

	my $switch = $schema->resultset('Switch')->update_or_create({
			host_code => $host_code,
			descr 	  => $descr,
			id_type	  => $types{$type}
		});

	print $switch->id, "\n";
}


	

#print Dumper(\%types, @switches);







